/*
 * Copyright (c) 2007, Nubo Technologies
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *
 *    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *    * Neither the name of the Nubo Technologies nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.nubotech.gwt.oss.browser.client.ui;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 *
 * @author jonnakkerud
 */
public class WidgetFactory {
    
    public static ClickableWidget createAnchor(String text) {
        ClickableWidget w = new ClickableWidget(DOM.createAnchor()) {  
        };
        DOM.setElementProperty(w.getElement(), "href", "javascript:;");
        DOM.setInnerText(w.getElement(), text);
        return w;
    }

    public static ClickableWidget createQuickCloseButton() {
        ClickableWidget w = new ClickableWidget(DOM.createDiv()) {  
        };
        w.setStyleName("quick-close");
        //DOM.setElementProperty(w.getElement(), "href", "javascript:;");
        DOM.setInnerText(w.getElement(), "x");
        return w;
    }
    
    /*public static PopupPanel createPopup(final Widget parent, Widget w) {
        final PopupPanel popup = new PopupPanel(true);
        popup.setWidget(w);
        popup.setPopupPositionAndShow(new PopupPanel.PositionCallback() {
          public void setPosition(int offsetWidth, int offsetHeight) {
            int left = parent.getOffsetWidth() - offsetWidth;
            int top = parent.getOffsetHeight() - offsetHeight;
            popup.setPopupPosition(left, top);
          }
        });
        return popup;
    }*/
    
    public static Widget createFloatWidget(String name, Widget innerWidget, ClickListener cl) {
        VerticalPanel floatWidget = new VerticalPanel();
        floatWidget.setWidth("100%");
        
        // name + close
        FlowPanel fp = new FlowPanel();
        floatWidget.add(fp);
        
        fp.add(new Spanner(name));
        
        ClickableWidget closeBtn = createQuickCloseButton();
        closeBtn.addClickListener(cl);
        fp.add(closeBtn);
        
        // inner widget        
        floatWidget.add(innerWidget);
        
        return floatWidget;
    }
    
    
    public static String imageWithText(AbstractImagePrototype image, String text) {
        return "<div style='white-space:nowrap;'>" +
                image.getHTML() +
                text +
                "</div>";
    }

    public static String createAnchor(String name, String url) {
        StringBuilder sb = new StringBuilder();
        sb.append("<a href=\"").append(url).append("\">");
        sb.append(name).append("</a>");
        return sb.toString();
    }
    
    public static class Spanner extends Widget {
        public Spanner(String text) {
            setElement(DOM.createSpan());
            DOM.setInnerText(getElement(), text);
            setStyleName("Spanner");
        }
    }
    
    
}
